package com.example.demo.controller.client;

import com.example.demo.model.CategoryModel;
import com.example.demo.model.FriendsLinkModel;
import com.example.demo.model.LabelModel;
import com.example.demo.services.*;
import com.example.demo.utils.Response;
import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("tools")
public class ToolsController {

    @Resource
    ArticleService articleService;
    @Resource
    CategoryService categoryService;
    @Resource
    LabelService labelService;
    @Resource
    FriendsLinkService friendsLinkService;

    @ModelAttribute
    public void commonModelAttribute(Model model) {
        // 获取最新的六篇文章
        PageInfo newestSixArticleList = articleService.getArticlePageList(1, 6);
        // 获取文章分类
        List<CategoryModel> categoryList = categoryService.getCategoryList();
        // 获取最热门的十个标签
        List<LabelModel> recommendLabelList = labelService.getRecommendLabelList();
        // 友链
        List<FriendsLinkModel> friendsLinkList = friendsLinkService.getAllFriendsLinkList("sort desc");

        model.addAttribute("newestSixArticleList", newestSixArticleList);
        model.addAttribute("recommendLabelList", recommendLabelList);
        model.addAttribute("categoryList", categoryList);
        model.addAttribute("friendsLinkList", friendsLinkList);
        model.addAttribute("categoryId", 100);
    }

    @RequestMapping("getFinalShellPassDecodeView")
    public ModelAndView getFinalShellPassDecodeView() {
        return new ModelAndView("client/tools/getFinalShellPassDecodeView");
    }

    @RequestMapping("getFinalShellPassDecode")
    Object getFinalShellPassDecode(
            @RequestParam(name = "pass", value = "") String password
    ) {
        try {
            HashMap<String, String> result = new HashMap<>();
            String pass = FinalShellService.decodePass(password);
            result.put("password", pass);
            return Response.Success("Success", result);
        }catch (Exception e) {
            return Response.Error("密文无效");
        }
    }

    @RequestMapping("getIncomeCount")
    public ModelAndView getIncomeCount() {
        return new ModelAndView("client/tools/getIncomeCount");
    }

}
